package bairui.international.weixin.portal.miniapp.timer;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sh_lianjing.task.timer.ExpireTaskHandler;
import com.sh_lianjing.task.timer.ExpireTasker;
import com.sh_lianjing.task.timer.TaskData;

import bairui.international.model.domain.jpa.TB_ProductOrder;
import bairui.international.model.domain.jpa.TB_SystemConfig;
import bairui.international.model.status.State_ProductOrder;
import bairui.international.service.Service_ProductOrder;
import bairui.international.service.Service_SystemConfig;

@Component
public class TimerTasks_ProductOrder {

	private final static long expire = 1000 * 60 * 60 * 24 * 1;	// 1 天
//	private final static long expire = 1000 * 60 * 30;		//1分钟
	
	@Autowired private Service_ProductOrder service_ProductOrder;
	@Autowired private Service_SystemConfig service_SystemConfig;
	final private ExpireTasker<TB_ProductOrder> tasker = new ExpireTasker<>();
	
	@PostConstruct
	public void post() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException{
		TB_SystemConfig bean = service_SystemConfig.findFirst();
//		expire = bean.getOrderExpireTime();
		//启动时先扫表
//			service_SystemConfig
		List<MyTaskData> taskDatas = service_ProductOrder
			.listExpireTaskOrder()
			.stream()
			.map(it->new MyTaskData(it))
			.collect(Collectors.toList());
		tasker.putAll(taskDatas);
		//执行
		tasker.start(1,new ExpireTaskHandler<TB_ProductOrder>() {
			@Override
			public void run(TB_ProductOrder order) {
				try {
					order = service_ProductOrder.load(order);
					if(order.getState() == State_ProductOrder.Paying){
						service_ProductOrder.cancel(order.getId());
					}
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		});
	}
	
	public void put(TB_ProductOrder order) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException{
		tasker.put(new MyTaskData(order));
	}
	
	/*
	 * 数据
	 */
	public static class MyTaskData extends TaskData<TB_ProductOrder>{

		public MyTaskData(TB_ProductOrder data) {
			super(data);
		}

		@Override
		public int compareTo(Delayed o) {
			Date thisSubmitDate = this.getData().getCreateTime();
			Date thatSubmitDate = ((MyTaskData)o).getData().getCreateTime();
			if(thatSubmitDate.before(thisSubmitDate)){
				return 1;
			}else if(thatSubmitDate.after(thisSubmitDate)){
				return -1;
			}
			return 0;
		}

		@Override
		public long getDelay(TimeUnit unit) {
			return unit.convert(this.getData().getCreateTime().getTime() + expire - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
		}

	}
	
}
